package com.xb.pool;

import java.util.TreeMap;
import java.util.concurrent.*;

/**
 * @program: register-jerry
 * @description: 线程池管理demo
 * @author: xu.bin
 * @create: 2020-11-22
 */
public class ExcutorsPoolDemo {

    static final Integer POOL_NUMS=10;
    public static  void main(String []args){
        /**
         * 1、corePoolSize线程池的核心线程数
         * 2、maximumPoolSize能容纳的最大线程数
         * 3、keepAliveTime空闲线程存活时间
         * 4、unit 存活的时间单位
         * 5、workQueue 存放提交但未执行任务的队列
         * 6、threadFactory 创建线程的工厂类
         * 7、handler 等待队列满后的拒绝策略
         */
        ThreadPoolExecutor threadPool=new ThreadPoolExecutor(2,5,
                1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for(int i=0;i<POOL_NUMS;i++){
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("线程池开始执行任务 Thread:"+Thread.currentThread());
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
        threadPool.shutdown();
    }
}